﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace MarioArena.World
{
    class Crosshair
    {
        /// <summary>
        /// The normalized direction of the crosshair with respect the the player's projectile origin, 
        /// to which a projectile can be moved.
        /// </summary>
        public Vector2 Direction; //Can be used as direction for for bullet, so add this to bullet position each update.
        /// <summary>
        /// The positionvector of the crosshair on the screen.
        /// </summary>
        public Vector2 Position;
        /// <summary>
        /// The sensitivity of the crosshairmovement. Default is 1.0f.
        /// </summary>

        private const int WIDTH = 24;
        private const int HEIGHT = 24;
 
        private Vector2 mHalfCrosshairSize;
        private Texture2D mTexture;

        private int mScreenWidth;
        private int mScreenHeight;

        public Crosshair(Player player)
        {
            this.mTexture = GameManager.Instance.CrosshairTexture;
            mHalfCrosshairSize = new Vector2(this.mTexture.Width / 2, this.mTexture.Height / 2);  

            this.mScreenWidth = GameManager.GraphicsDevice.Viewport.Width;
            this.mScreenHeight = GameManager.GraphicsDevice.Viewport.Height;
        }

        /// <summary>
        /// Draws the crosshair.
        /// </summary>
        public void Draw(SpriteBatch spriteBatch)
        {
            // Draw the crosshair
            spriteBatch.Draw(this.mTexture, this.Position - this.mHalfCrosshairSize, Color.White);
        }

        /// <summary>
        /// Updates the crosshair.
        /// </summary>
        public void Update(bool isActive)
        {
            if (isActive)
            {
                CalculateCrosshair();
            }
        }

        /// <summary>
        /// Calculates the crosshair's position and direction.
        /// </summary>
        private void CalculateCrosshair()
        {
            MouseState mouseState = Mouse.GetState();

            int x = mouseState.X;
            int y = mouseState.Y;

            // Keep the mouse within the screen
            if (x < 0)
                x = 0;
            else if (x > this.mScreenWidth)
                x = this.mScreenWidth;
            if (y < 0)
                y = 0;
            else if (y > this.mScreenHeight)
                y = this.mScreenHeight;

            Mouse.SetPosition(x, y);
            this.Position = new Vector2(x, y);
            this.Direction = this.Position - Player.Instance.AbsoluteProjectileOrigin;
            this.Direction.Normalize();
        }

    }
}
